<?php
namespace Customer\Controller;

use Base\Controller\AdminBaseController;

class CustomerController extends AdminBaseController {

	protected $customerModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->customerModel = D("Customer/Customer");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$customerJson = file_get_contents('php://input');
		$customer = json_decode($customerJson, true);
		
		// 检查用户名是否重复
		$oldCustomer = $this->customerModel->where(array("username" => $customer['username']))->find();
		if ($oldCustomer) {
			$data['code'] = 499;
			$data['msg'] = '用户名重复，添加失败！';
			$this->ajaxReturn($data);
		}

		// setting
// 		$customer['status'] = 0;
		$customer['password'] = sf_xcms_password('xcms');
		$customer['add_time'] = Date('Y-m-d H:i:s');
		$customer['update_time'] = Date('Y-m-d H:i:s');

		// 执行添加
		$result = $this->customerModel->data($customer)->add();
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newCustomer = $this->customerModel->where(array("id" => $result))->find();
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newCustomer;
		$this->ajaxReturn($data);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$customer = $this->customerModel->where(array("id" => $id))->find();
		if (!$customer) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $customer;
	    $this->ajaxReturn($data);
	}

	/** 查看所有列表 */
	public function dataList() {
		$_GET['notEqualMap'] = ['status' => -1];
		$dataList = $this->customerModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$customerJson = file_get_contents('php://input');
		$customer = json_decode($customerJson, true);

		// update
// 		$customer['status'] = 0;										// update reset
		$customer['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->customerModel->where(array("id" => $id))->save($customer);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedCustomer = $this->customerModel->where(array("id" => $id))->find();
			
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $customer;
		$this->ajaxReturn($data);
	}

	/** 审核 */
	public function audit() {
		$id = $_GET['id'];
		$status = $_GET['status'];
		
		$customer = $this->customerModel->where(array("id" => $id))->find();
		if (!$customer) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$customer['status'] = $status;
		$customer['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->customerModel->where(array("id" => $id))->save($customer);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '审核失败！';
			$this->ajaxReturn($data);
		}
			
		$data['code'] = 0;
		$data['msg'] = '审核完成！';
		$data['obj'] = $customer;
		$this->ajaxReturn($data);
	}

	/** 为会员绑定会员卡 */
	public function bindCard() {
		$id = $_GET['id'];
		$cardNo = $_GET['cardNo'];
		
		$customer = $this->customerModel->where(array("id" => $id))->find();
		if (!$customer) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		////// unbind and bind
		if ($cardNo == "unbind") {
            ////// unbing
    		// 更新会员卡
            M('Card')->where(array("id" => $customer['card_id']))->setDec('cur_bind_count', 1);
            
            // 解绑会员卡
            $customer['card_id'] = 0;
            $customer['card_no'] = '';
            $customer['card_lv'] = 0;
            $customer['card_expiry_time'] = '1000-01-01 00:00:00';
    		$customer['update_time'] = Date('Y-m-d H:i:s');
    		
    		$result = $this->customerModel->where(array("id" => $id))->save($customer);
    		if (false === $result) {
    			$data['code'] = 499;
    			$data['msg'] = '解绑失败！';
    			$this->ajaxReturn($data);
    		}
    
            $data['code'] = 0;
            $data['msg'] = '解绑成功!';
            $data['obj'] = $customer;
            $this->ajaxReturn($data);
		} else {		
    		////// bing
    		// 判断会员卡是否存在
    		$card = M('Card')->where(array("no" => $cardNo))->find();
    		if (!$card) {
    		    $data['code'] = 499;
    		    $data['msg'] = '未找到会员卡!';
    		    $this->ajaxReturn($data);
    		}
    
    		// 判断会员卡绑定数量是否已满
    		if ($card['cur_bind_count'] >= $card['max_bind_count']) {
    		    $data['code'] = 499;
    		    $data['msg'] = '会员卡绑定数量已满!';
    		    $this->ajaxReturn($data);
    		}
    		
    		// 如果之前绑定过会员卡，则解除绑定
            $oldCard = M('Card')->where(array("id" => $customer['card_id']))->find();
    		if ($oldCard) {
    		    M('Card')->where(array("id" => $customer['card_id']))->setDec('cur_bind_count', 1);

    		    $customer['card_id'] = 0;
    		    $customer['card_no'] = '';
    		    $customer['card_lv'] = 0;
    		    $customer['card_expiry_time'] = '1000-01-01 00:00:00';
    		    $customer['update_time'] = Date('Y-m-d H:i:s');
    		    
    		    $result = $this->customerModel->where(array("id" => $id))->save($customer);
    		    if (false === $result) {
    		        $data['code'] = 499;
    		        $data['msg'] = '解绑失败！';
    		        $this->ajaxReturn($data);
    		    }
    		}
    		
    		// 更新会员卡
            M('Card')->where(array("id" => $card['id']))->setInc('cur_bind_count', 1);
    
            // 绑定会员卡
            $customer['card_id'] = $card['id'];
            $customer['card_no'] = $card['no'];
            $customer['card_lv'] = $card['lv'];
            $customer['card_expiry_time'] = $card['expiry_time'];
    		$customer['update_time'] = Date('Y-m-d H:i:s');
    		
    		$result = $this->customerModel->where(array("id" => $id))->save($customer);
    		if (false === $result) {
    			$data['code'] = 499;
    			$data['msg'] = '绑定失败！';
    			$this->ajaxReturn($data);
    		}
    
            $data['code'] = 0;
            $data['msg'] = '绑定成功!';
            $data['obj'] = $customer;
            $this->ajaxReturn($data);
    	}
	}
	
	/** 删除某信息（禁用） */
	public function delete() {
		$id = $_GET['id'];
		
		$customer = $this->customerModel->where(array("id" => $id))->find();
		if (!$customer) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		////// 如果绑定了会员卡，则删除失败
		if ($customer['card_id']) {
		    $data['code'] = 499;
		    $data['msg'] = '删除失败，已绑定会员卡！';
		    $this->ajaxReturn($data);
		}

		////// delete (更新status & update)
		$customer['status'] = -1;
		$customer['update_time'] = date("Y-m-d H:i:s");

		$result = $this->customerModel->where(array("id" => $id))->save($customer);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}
		
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	// index
    public function index() {
		$this->display(':customer');
		return;
	}
}
